package com.example.jpa.controller;

import com.example.jpa.entity.Programmer;
import com.example.jpa.service.ProgrammerService;
import com.example.jpa.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("programmer")
public class ProgrammerController {

    @Autowired
    private ProgrammerService programmerService;


    @PostMapping
    public Object save(Programmer programmer){
        Programmer result = programmerService.save(programmer);
        return ResultVo.builder().code(200).message("success!").data(result).build();
    }

    @GetMapping
    public Object list(Programmer programmer){
        List<Programmer> list = programmerService.getList(programmer);
        return ResultVo.builder().code(200).message("success!").data(list).build();
    }

    @GetMapping("{id}")
    public Object one(@PathVariable Long id){
        Programmer programmer = programmerService.findById(id);
        return ResultVo.builder().code(200).message("success!").data(programmer).build();
    }

    @PutMapping
    public Object update(Programmer programmer){
        Programmer result = programmerService.update(programmer);
        return ResultVo.builder().code(200).message("success!").data(result).build();
    }
//    一个一个删
//    @DeleteMapping
//    public Object delete(Long id){
//        programmerService.deleteById(id);
//        return ResultVo.builder().code(200).message("success!").build();
//    }
    @DeleteMapping
    public Object delete(@RequestParam("id") List<Long> ids){
        System.out.println("ids==>"+ ids);
        programmerService.deleteByIds(ids);
        return ResultVo.builder().code(200).message("success!").build();
}


}
